package com.example.goodpet.my.adapter;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.goodpet.R;
import com.example.goodpet.activity.PetInfoActivity;
import com.example.goodpet.bean.PetInformation;
import com.example.goodpet.bean.ResponseBody;
import com.example.goodpet.bean.User;
import com.example.goodpet.bean.UserPetInfo;
import com.example.goodpet.db.DatabaseHelper;
import com.example.goodpet.my.fragment.MyPetInfoFragment;
import com.example.goodpet.service.PetInformationService;
import com.example.goodpet.utils.AppNetConfig;
import com.example.goodpet.utils.Constants;
import com.example.goodpet.utils.MyApplication;
import com.example.goodpet.utils.UIUtils;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class MyReleasePetAdapter extends RecyclerView.Adapter<MyReleasePetAdapter.ViewHolder> {
    private List<PetInformation> lists;
    private Context context;
    private MyPetInfoFragment myPetInfoFragment;
    public MyReleasePetAdapter(List<PetInformation> lists, Context context,MyPetInfoFragment myPetInfoFragment) {
        this.lists = lists;
        this.context=context;
        this.myPetInfoFragment=myPetInfoFragment;
    }

    @NonNull
    @Override
    public MyReleasePetAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View inflate = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_my_release, parent,false);
        return new ViewHolder(inflate);
    }

    @Override
    public void onBindViewHolder(@NonNull MyReleasePetAdapter.ViewHolder holder, int position) {

        PetInformation petInformation = lists.get(position);
        holder.tvPostdate.setText(petInformation.getPostdate().toString());
        holder.tvOverView.setText(petInformation.getOverview());
        Glide.with(context).load(Constants.BASE_URL_IMAGE+petInformation.getPetImage().get(0)).into(holder.petPhoto);

        initListener(holder,position);
    }


        private void initListener(@NonNull MyReleasePetAdapter.ViewHolder holder, int position) {
            holder.tvDelete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    new AlertDialog.Builder(context)
                            .setTitle("温馨提示")
                            .setMessage("您确定要删除吗？")
                            .setPositiveButton("删除", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    //连接服务器,传送宠物id
                                    Retrofit build = new Retrofit.Builder().baseUrl(AppNetConfig.BASE_URL).addConverterFactory(GsonConverterFactory.create()).build();
                                    PetInformationService petInformationService = build.create(PetInformationService.class);
                                    petInformationService.deleteInformation(lists.get(position).getId()).enqueue(new Callback<ResponseBody<Object>>() {
                                        @Override
                                        public void onResponse(Call<ResponseBody<Object>> call, Response<ResponseBody<Object>> response) {
                                            ResponseBody<Object> body = response.body();
                                            System.out.println("MyReleaseAdapter.onResponse"+body);
                                            int code = response.body().getCode();
                                            if (code==200){
                                                DatabaseHelper.getInstance(UIUtils.getContext()).getGoodpetHistoryDAO().deleteByUserIdAndPetId(MyApplication.user.getId(),lists.get(position).getId());
                                                myPetInfoFragment.notifyDataList(position);
                                                //lists.remove(position);
                                                //刷新
                                                //notifyItemRemoved(position);
                                                //notifyDataSetChanged();
                                            }else if(code==500){
                                                UIUtils.toast("服务器错误",false);
                                            }
                                        }

                                        @Override
                                        public void onFailure(Call<ResponseBody<Object>> call, Throwable t) {
                                            UIUtils.toast("连接服务器失败",false);
                                        }
                                    });

                                }
                            })
                            .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    dialog.cancel();
                                }
                            })
                            .setCancelable(false).show();
                }
            });
            holder.layoutEnter.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(context, PetInfoActivity.class);
                    PetInformation petInformation = lists.get(position);
                    User user=MyApplication.user;
                    UserPetInfo userPetInfo = new UserPetInfo(user.getId(), user.getPhoneNumber(),  user.getNickname(),  user.getPortrait(),user.getGender(),user.getSignature(),petInformation.getId(), petInformation.getCategory(), petInformation.getOverview(), petInformation.getPlace(),petInformation.getPostdate().toString(),petInformation.getType(),petInformation.getPetImage());
                    /*intent.putExtra("petInfo",pet);
                    intent.putExtra("user",MyApplication.user);*/
                    intent.putExtra("userPetInfo",userPetInfo);
                    context.startActivity(intent);
                }
            });
//            holder.tvEdit.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View v) {
//                    new Intent();
//                }
//            });
        }


    @Override
    public int getItemCount() {
        return lists.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder{
        TextView tvPostdate;
        TextView tvOverView;
        ImageView petPhoto;
        TextView tvEdit;
        TextView tvDelete;
        LinearLayout layoutEnter;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            tvPostdate=itemView.findViewById(R.id.tvPostdate);
            tvOverView=itemView.findViewById(R.id.tvOverView);
            petPhoto=itemView.findViewById(R.id.petPhoto);
            //tvEdit=itemView.findViewById(R.id.tvEdit);
            tvDelete=itemView.findViewById(R.id.tvDelete);
            layoutEnter=itemView.findViewById(R.id.layoutEnter);


        }


    }

}
